package cn.kgc.controller;

import cn.kgc.pojo.Branch;
import cn.kgc.pojo.Pdm;
import cn.kgc.pojo.position;
import cn.kgc.service.PdmService;
import cn.kgc.until.SzbUpload;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


/**
 * @author: 苏志彪
 * @date: 2022/2/14
 * @description:
 */
@Controller
public class RsController {

    @Resource
    private PdmService pdmservice;

    /**
     * 跳转人事修改页面,数据跳转
     * @return
     */
    @RequestMapping("/renshixiugai/{id}")
    public String renshixiugai(@PathVariable Integer id,
                               Model model){
        List<Pdm> pdms = pdmservice.fuhPdm(id);
        model.addAttribute("pdms",pdms);
        return "yemian/renshi/renshixiugai";
    }

    /**
     * 跳转人事登记页面
     * @return
     */
    @RequestMapping("/renshidengji")
    public String renshidengji(){
        return "yemian/renshi/renshidengji";
    }

//    /**
//     * 添加人事档案su
//     * @param pdm
//     * @return
//     */
//    @RequestMapping("/tianjiadangans")
//    @ResponseBody
//    public String tianjiadangan(@ModelAttribute Pdm pdm){
//        boolean bo = pdmservice.addrenshi(pdm);
//        if (bo == true){
//            System.out.println("添加成功");
//        }else{
//            System.out.println("添加失败");
//        }
//        return JSONObject.toJSONString(bo);
//    }

    /*@RequestMapping("/xuan")
    @ResponseBody
    public String xuan(){
        List<position> list = pdmservice.selposition();
        return JSON.toJSONString(list);
    }*/

    //suzhibiao人事添加  文件上传
    @PostMapping("/renshitijiaoa1")
    @ResponseBody
    public Boolean upload(Pdm pdm,
                          @RequestParam("file") MultipartFile file,
                          @RequestParam("imggggg") MultipartFile imggggg) throws IOException {

        //订单编号

        long timeMillis = System.currentTimeMillis();
        String code = "BDQN" + timeMillis;

        //  System.out.println("jobTitle===================="+jobTitle);

        // position getlists = pdmservice.getlists(jobTitle);

        //System.out.println("获得position类的id-----------------"+getlists.getId());


        System.out.println("pdm = " + pdm);

        //获得文件的名称
        String filename = file.getOriginalFilename();
        String property = System.getProperty("user.dir") + "\\src\\main\\resources\\upload\\szb\\" + filename;

        String img = imggggg.getOriginalFilename();
        String property1 = System.getProperty("user.dir") + "\\src\\main\\resources\\upload\\szb\\" + img;

        //调用我们写的工具类
        SzbUpload.upload(file);//储存文件
        SzbUpload.upload(imggggg);//储存文件

        pdm.setCheckStatus("起草");
        pdm.setResumeName(filename);
        pdm.setFileCoding(code);
        pdm.setPhonePath(property1);
        pdm.setResumeAttachment(property);
        pdm.setResumeName(filename);
        //pdm.setJobTitle(getlists.getId());

        boolean b = pdmservice.addrenshi(pdm);

        return b;    // 文件上传后，发送到js
    }

    /**
     * 添加人事档案su
     * @param pdm
     * @return
     */
    @RequestMapping("/tianjiadangans")
    @ResponseBody
    public String tianjiadangan(@ModelAttribute Pdm pdm){
        boolean bo = pdmservice.addrenshi(pdm);
        if (bo == true){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return JSONObject.toJSONString(bo);
    }

    /**
     * 下拉框
     * @return
     */
    @RequestMapping("/zuzhiyes")
    @ResponseBody
    public String zuzhiye(){

        List<Branch> branches = pdmservice.XiaLaKuangBranch();

        return JSONObject.toJSONString(branches);
    }

    @RequestMapping("/zuzhis")
    @ResponseBody
    public String zuzhifenye(@RequestParam Integer currentPage,
                             @RequestParam Integer pageSize,@RequestParam String posName,
                             @RequestParam String posCode,
                             @RequestParam String brName){

        PageHelper.startPage(currentPage,pageSize);
        Page<position> branchList = pdmservice.getBranchLists(posName,posCode,brName);
        PageInfo<position> positionPageInfo = new PageInfo<>(branchList);

        return JSONObject.toJSONStringWithDateFormat(positionPageInfo,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     *
     * 渲染职位名称，职位分类，职位编码
     * @param id
     * @return
     */
    @RequestMapping("/zhiyefensss")
    @ResponseBody
    public String zhiye(@RequestParam Integer id){
        position positionFen = pdmservice.getzhiwei(id);
        return JSONObject.toJSONString(positionFen);
    }
}
